package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Hotel;
import cn.wolfcode.luowowo.article.mapper.HotelMapper;
import cn.wolfcode.luowowo.article.query.HotelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IHotelService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class HotelServiceImpl implements IHotelService {

    @Autowired
    private HotelMapper hotelMapper;
    @Reference
    private IDestinationService destinationService;

    @Override
    public PageInfo query(HotelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(hotelMapper.selectForList(qo));
    }

    @Override
    public void saveOrUpdate(Hotel hotel) {
        if(hotel.getId() == null){
            hotelMapper.insert(hotel);
        }else{
            hotelMapper.updateByPrimaryKey(hotel);
        }
    }

    @Override
    public Hotel get(Long id) {
        return hotelMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<Hotel> queryHotelByDestId(Long id) {
        return hotelMapper.selectHotelByDestId(id);
    }

    @Override
    public List<Hotel> getHotelByThemeId(Long id) {
        return hotelMapper.selectHotelByThemeId(id);
    }

    @Override
    public List<Hotel> getHotelByThemeByDestName(String cityName) {
        Destination destination = destinationService.getCityByName(cityName);
        if (destination == null ){
            return null;
        }
        List<Hotel> hotels = hotelMapper.selectHotelByDestId(destination.getId());
        return hotels;
    }

    @Override
    public List<Hotel> getHotelByThemeIdAndDestId(HotelQuery qo) {

        return hotelMapper.selectHotelByThemeIdAndDestId(qo);
    }

    @Override
    public List<Hotel> getHotelByDestName(String name) {
        return hotelMapper.selectHotelByDestName(name);
    }
}
